package tk3.project;

import java.io.File;
import java.util.ArrayList;

import tk3.project.uploadservice.Client;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;

public class NewImageObservationService extends Service {

	//private ImageDirectoryObserver observer;
	private ArrayList<ImageDirectoryObserver> observers = new ArrayList<ImageDirectoryObserver>();
	private PictureReceiverThread receiverThread=null;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub 
		return null;
	}
	private Client client;

	@Override
	public void onCreate() {
		
		//create new upload client
		client=new Client(Settings.getActiveInstance().getIP(), Settings.getActiveInstance().getPort(), Settings.getActiveInstance().getPath());
		client.start();
				
		//instantiate and start image receiving thread
				
				
		receiverThread = new PictureReceiverThread(client,Settings.getActiveInstance().getInterval());
		receiverThread.start();
		
		//instantiate and register image sending gps listener
		if(Settings.getActiveInstance().isGpsBased()){
			startLocationListener();
		}
		//instantiate and register file observers
		File pictureFileDir = getDir();
		
		for(File subfile : pictureFileDir.listFiles()){
			if(subfile.isDirectory()){
				ImageDirectoryObserver observer = new ImageDirectoryObserver(subfile.getPath(), FileObserver.CREATE,client);
				observers.add(observer);
				observer.startWatching();
			}
				
		}
		
		
	}
	/**
	 * 
	 * @return The folder where pictures are saved
	 */
	private File getDir() {
		return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

	}
	
	@Override
	public void onDestroy(){
		stopLocationListener();
		if(receiverThread!=null)
			receiverThread.stopReceiver();
			
			
			client.stopClient();
		super.onDestroy();
	}
	
	/**
	 * Enables the location Listener
	 */
	private void startLocationListener(){
		GPSPictureSender gpsPicSender = GPSPictureSender.getInstance(client);
		LocationManager locManager = (LocationManager) this.getBaseContext().getSystemService(Context.LOCATION_SERVICE);
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsPicSender);
		locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, gpsPicSender);
		System.out.println("Receiver thread: "+receiverThread);
		locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, receiverThread);
		locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, receiverThread);
	}
	
	/**
	 * Disables the Location Listener
	 */
	private void stopLocationListener(){
		GPSPictureSender gpsPicSender=GPSPictureSender.getInstance(client);
		LocationManager locManager = (LocationManager) this.getBaseContext().getSystemService(Context.LOCATION_SERVICE);
		locManager.removeUpdates(gpsPicSender);
		locManager.removeUpdates(receiverThread);
	}
	


}
